package com.ios.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ios.model.entity.Banner ;
import com.ios.service.BannerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 轮播图管理Controller
 * 
 * @author ios
 * @date 2025-04-13
 */
@RestController
@RequestMapping("/banner")
public class BannerController extends BaseController
{
    @Autowired
    private BannerService bannerService;

    /**
     * 查询轮播图管理列表
     */
//    @PreAuthorize("@ss.hasPermi('ios:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(Banner banner)
    {
        startPage();
        List<Banner> list = bannerService.selectBannerList(banner);
        return getDataTable(list);
    }

    /**
     * 导出轮播图管理列表
     */
//    @PreAuthorize("@ss.hasPermi('ios:banner:export')")
    @Log(title = "轮播图管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Banner banner)
    {
        List<Banner> list = bannerService.selectBannerList(banner);
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        util.exportExcel(response, list, "轮播图管理数据");
    }

    /**
     * 获取轮播图管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('ios:banner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bannerService.selectBannerById(id));
    }

    /**
     * 新增轮播图管理
     */
//    @PreAuthorize("@ss.hasPermi('ios:banner:add')")
    @Log(title = "轮播图管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Banner banner)
    {
        return toAjax(bannerService.insertBanner(banner));
    }

    /**
     * 修改轮播图管理
     */
//    @PreAuthorize("@ss.hasPermi('ios:banner:edit')")
    @Log(title = "轮播图管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Banner banner)
    {
        return toAjax(bannerService.updateBanner(banner));
    }

    /**
     * 删除轮播图管理
     */
//    @PreAuthorize("@ss.hasPermi('ios:banner:remove')")
    @Log(title = "轮播图管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bannerService.deleteBannerByIds(ids));
    }

    @PostMapping("/uploadCover")
    public AjaxResult uploadCover(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return error("上传文件不能为空");
            }
            // 验证文件类型
            String contentType = file.getContentType();
            if (!contentType.startsWith("image/")) {
                return error("只能上传图片文件");
            }
            // 文件大小限制 (1MB)
            if (file.getSize() > 1048576) {
                return error("文件大小不能超过1MB");
            }

            // 创建D盘的upload目录（如果不存在）
            String uploadDir = "D:/upload/";
            File dir = new File(uploadDir);
            if (!dir.exists()) {
                dir.mkdirs(); // 创建多级目录
            }

            // 存储文件到D:/upload/
            String fileName = UUID.randomUUID() + "." + StringUtils.getFilenameExtension(file.getOriginalFilename());
            String filePath = uploadDir + fileName;
            file.transferTo(new File(filePath));

            // 返回完整访问URL
            String baseUrl = "http://localhost:8080";
            String accessUrl = baseUrl + "/profile/upload/" + fileName;

            return AjaxResult.success("上传成功")
                    .put("url", accessUrl);
        } catch (Exception e) {
            return AjaxResult.error("上传失败: " + e.getMessage());
        }
    }
}
